package com.wifipositioner.measure;

import java.io.Serializable;

import com.wifipositioner.Globals;

public class AverageHelper implements Comparable<AverageHelper>, Serializable{
	private Double value;
	private int cntr;
	private double levelWeight;
	private int[] valueList;
	
	public AverageHelper() {
	}
	
	public AverageHelper(int value) {
		super();
		this.value = (double)value;
		this.cntr = 1;
		this.valueList = new int[Globals.maxMeasureCntr];
		this.valueList[cntr - 1] = value;
		setLevelWeight();
	}
	public Double getValue() {
		return value;
	}
	public void setValue(Double value) {
		this.value = value;
	}
	public int getCntr() {
		return cntr;
	}
	public void setCntr(int cntr) {
		this.cntr = cntr;
	}
	public void incrementCntr(){
		cntr++;
	}
	public double getAvg(){
		return value / cntr;
	}
	public void resetCntr(){
		cntr = 0;
	}
	public void addVal(int value){
		this.value += value;
		this.valueList[cntr] = value;
		cntr++;
		setLevelWeight();
	}

	@Override
	public int compareTo(AverageHelper another) {
		if(this.getAvg() == another.getAvg())
			return 0;
		else if(this.getAvg() > another.getAvg())
			return -1;
		else
			return 11;
	}
	public void setLevelWeight(){
		if(getAvg() < -90){
			levelWeight = Globals.weight90;
		}
		else if(getAvg() < -80){
			levelWeight = Globals.weight80;
		}
		else if(getAvg() < -70){
			levelWeight = Globals.weight70;
		}
		else if(getAvg() < -60){
			levelWeight = Globals.weight60;
		}
	}
	public double getLevelWeight() {
		return levelWeight;
	}
	public double getVariance(){
		double avg = getAvg();
		int sum = 0;
		for(int value : valueList){
			sum += (avg - value) * (avg - value);
		}
		sum /= cntr;
		if(sum == 0)
			return 1;
		return sum;
	}
	
}
